package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.CourseBo;
import com.tpyule.tpclass.entity.bo.CourseDetailBo;
import com.tpyule.tpclass.entity.bo.CourseManageBo;
import com.tpyule.tpclass.entity.dto.*;
import com.tpyule.tpclass.entity.pojo.Course;
import com.tpyule.tpclass.service.CourseService;
import com.tpyule.tpclass.service.StudyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/3/20 19:53
 */

@RestController
@RequestMapping("course")
public class CourseController extends BaseController {
    @Autowired
    CourseService courseService;
    @RequestMapping("/getCourse")
    @ResponseBody
    public void getCourse(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseParam = request.getParameter("courseData");
        try {
            CourseParamDto courseParamDto = objectMapper.readValue(courseParam,CourseParamDto.class);
            if(courseParamDto!=null){
                List<Course> courses = courseService.getAllCourse(courseParamDto);
                resultBean = new SuccessResultBean(courses,"获取成功");
            }else{
                resultBean = new FailResultBean("格式化错误");

            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getListCourse")
    @ResponseBody
    public void getListCourse(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String queryStr = request.getParameter("queryData");
        try {
            CourseManageQueryDto dto = objectMapper.readValue(queryStr,CourseManageQueryDto.class);
            if(dto!=null){
                List<CourseManageBo> courses = courseService.getListManageCourse(dto);
                resultBean = new SuccessResultBean(courses,"获取成功");
            }else{
                resultBean = new FailResultBean("格式化错误");

            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getSizeCourse")
    @ResponseBody
    public void getSizeCourse(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String queryStr = request.getParameter("queryData");
        try {
            CourseManageQueryDto dto = objectMapper.readValue(queryStr,CourseManageQueryDto.class);
            if(dto!=null){
                int result= courseService.getSizeManageCourse(dto);
                resultBean = new SuccessResultBean(result,"获取成功");
            }else{
                resultBean = new FailResultBean("格式化错误");

            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping("/getListAuditCourse")
    @ResponseBody
    public void getListAuditCourse(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String queryStr = request.getParameter("queryData");
        try {
            CourseManageQueryDto dto = objectMapper.readValue(queryStr,CourseManageQueryDto.class);
            if(dto!=null){
                List<CourseManageBo> courses = courseService.getListAuditCourse(dto);
                resultBean = new SuccessResultBean(courses,"获取成功");
            }else{
                resultBean = new FailResultBean("格式化错误");

            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getSizeAuditCourse")
    @ResponseBody
    public void getSizeAuditCourse(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String queryStr = request.getParameter("queryData");
        try {
            CourseManageQueryDto dto = objectMapper.readValue(queryStr,CourseManageQueryDto.class);
            if(dto!=null){
                int result= courseService.getSizeAuditCourse(dto);
                resultBean = new SuccessResultBean(result,"获取成功");
            }else{
                resultBean = new FailResultBean("格式化错误");

            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCourseByTId")
    @ResponseBody
    public void getCourseByTId(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseParam = request.getParameter("courseParam");
        try {
            CourseByTId courseByTId = objectMapper.readValue(courseParam, CourseByTId.class);
            if(courseByTId!=null){
                List<Course> courses = courseService.getListCourseByTeacherId(courseByTId);
                resultBean = new SuccessResultBean(courses,"获取成功");
            }else{
                resultBean = new FailResultBean("错误");

            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping("/addCourse")
    @ResponseBody
    public void addCourse(HttpServletRequest request, HttpServletResponse response) {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String courseData=request.getParameter("courseData");
            Course course=objectMapper.readValue(courseData, Course.class);
            String courseId = courseService.addCourse(course);
            resultBean = new SuccessResultBean<String>(courseId,"添加成功");
            resultBean = new SuccessResultBean<String>(courseId,"添加成功");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCourseCS")
    @ResponseBody
    public void getCourseCS(HttpServletRequest request, HttpServletResponse response) {
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseId = request.getParameter("courseId");
        try {
            if(courseId!=null){
                List<CourseCDto> courseCDtos = courseService.getCourseCS(courseId);
                if(courseCDtos!=null)
                    resultBean = new SuccessResultBean(courseCDtos,"获取成功");
                else
                    resultBean = new FailResultBean();
            }else{
                resultBean = new FailResultBean("请确认传入参数");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCourseById")
    @ResponseBody
    public void getCourseById(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseId = request.getParameter("courseId");
        try {
            if(courseId!=null){
                Course courses = courseService.getCourseById(courseId);
                resultBean = new SuccessResultBean(courses,"获取成功");
            }else{
                resultBean = new FailResultBean("错误");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCourseBoById")
    @ResponseBody
    public void getCourseBoById(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseId = request.getParameter("courseId");
        try {
            if(courseId!=null){
                CourseBo course = courseService.getCourseBoById(courseId);
                resultBean = new SuccessResultBean(course,"获取成功");
            }else{
                resultBean = new FailResultBean("错误");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/deleteCourseById")
    @ResponseBody
    public void deleteCourseById(HttpServletRequest request, HttpServletResponse response){
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseId = request.getParameter("courseId");
        try {
            if(courseId!=null){
                Boolean result = courseService.deleteCourseById(courseId);
                if (result){
                    resultBean = new SuccessResultBean<String>(courseId,"获取成功");
                }else {
                    resultBean = new FailResultBean("错误");
                }
            }else{
                resultBean = new FailResultBean("错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCourseDetail")
    @ResponseBody
    public void getCourseDetail(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseId = request.getParameter("courseId");
        try {
            if(courseId!=null){
                resultBean = courseService.getCourseDetail(courseId);
            }else{
                resultBean = new FailResultBean("错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCourseCsByUser")
    @ResponseBody
    public void getCourseCsByUser(HttpServletRequest request, HttpServletResponse response) {
        request.getSession();
        ObjectMapper objectMapper=new ObjectMapper();
        ResultBean resultBean=null;
        String courseId = request.getParameter("courseId");
        String userId = request.getParameter("userId");
        try {
            if(courseId!=null){
                List<CourseCDto> courseCDtos = courseService.getCourseCSByUser(courseId,userId);
                if(courseCDtos!=null)
                    resultBean = new SuccessResultBean(courseCDtos,"获取成功");
                else
                    resultBean = new FailResultBean();
            }else{
                resultBean = new FailResultBean("请确认传入参数");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                this.setAjaxResponseResult(response,resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCourseByQId")
    @ResponseBody
    public void getCourseByQId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String questionId = request.getParameter("questionId");

            if(questionId==null||"".equals(questionId)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                Course course = courseService.getCourseByQId(questionId);
                if (course!=null)
                    resultBean = new SuccessResultBean(course, "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getCourseToShow")
    @ResponseBody
    public void getCourseToShow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String userId = request.getParameter("userId");

            if(userId==null||"".equals(userId)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                List<Course> courses = courseService.getCourseToShow(userId);
                if (courses!=null)
                    resultBean = new SuccessResultBean(courses, "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/updateCourseState")
    @ResponseBody
    public void updateCourseState(HttpServletRequest request, HttpServletResponse response) {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String courseData=request.getParameter("courseData");
            CourseStateDto dto =objectMapper.readValue(courseData, CourseStateDto.class);
            int result = courseService.updateCourseState(dto);
            if (result!=0)
             resultBean = new SuccessResultBean("成功");
            else {
                resultBean = new FailResultBean();
            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/updateCourse")
    @ResponseBody
    public void updateCourse(HttpServletRequest request, HttpServletResponse response) {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String courseData=request.getParameter("courseData");
            CourseBo bo =objectMapper.readValue(courseData, CourseBo.class);
            int result = courseService.updateCourseDetail(bo);
            if (result!=0)
                resultBean = new SuccessResultBean("成功");
            else {
                resultBean = new FailResultBean();
            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
